<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="text" id = "message">
<button id="send-button">发送</button>

<script>
    let websocket = new WebSocket("ws://127.0.0.1:8080/test");

    websocket.onopen = function (){
        console.log("连接成功");
    }

    websocket.onmessage = function (e){
        console.log("收到消息 ："+e.data);
    }

    websocket.onclose = function (){
        console.log("连接断开");
    }

    websocket.onerror = function (){
        console.log("连接异常，断开");
    }

    let sendButoon = document.querySelector("#send-button");
    let messageinput = document.querySelector("#message");
    sendButoon.onclick = function (){
        console.log("websocket 发送消息:"+messageinput.value);
        websocket.send(messageinput.value);
    }
</script>
</body>
</html>